package com.qfedu.predicates;

import io.jsonwebtoken.*;
import io.jsonwebtoken.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.function.Predicate;

/**
 * <p>title: com.wfx.predicates</p>
 * author zhuximing
 * description:   需求：年龄必须是18以上
 */
@Component
public class MyHeaderRoutePredicateFactory extends AbstractRoutePredicateFactory<MyConfig> {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public MyHeaderRoutePredicateFactory() {
        super(MyConfig.class);
    }




    @Override
    public Predicate<ServerWebExchange> apply(MyConfig config) {
        return new Predicate<ServerWebExchange>() {
            @Override
            public boolean test(ServerWebExchange exchange) {
                String token = exchange.getRequest().getHeaders().getFirst("token");

                if(token == null){
                    return false;
                }

                //检查令牌在redis缓存中是否命中
                if (!stringRedisTemplate.hasKey(token)){
//                    ResultVo illegal = ResultVo.loginStatus(ResultStatus.ILLEGAL,"令牌不合法");
//                    doResponse(response,illegal);
                    return false;
                }

                try {
                    Long expire = stringRedisTemplate.boundSetOps(token).getExpire();       //打印当前令牌剩余时间
                    System.out.println("剩余时间:"+expire);
                    String s = stringRedisTemplate.opsForValue().get(token);                //取出value
                    System.out.println("value:"+s);
                    JwtParser parser = Jwts.parser();                                       //jwt令牌解析
                    parser.setSigningKey("123456");
                    Jws<Claims> claimsJws = parser.parseClaimsJws(token);
                    stringRedisTemplate.expire(token,30, TimeUnit.MINUTES);          //重置令牌缓存过期时间
                    Long expire1 = stringRedisTemplate.boundSetOps(token).getExpire();      //打印重置缓存过期时间后，令牌的剩余时间
                    System.out.println("剩余时间:"+expire1);
                    return true;
                }catch (ExpiredJwtException e){
                    e.printStackTrace();
                }catch (UnsupportedJwtException e){
                    e.printStackTrace();
                }catch (Exception e){
                    e.printStackTrace();
                }
                return false;
            }
        };
    }

    //获取配置参数
    @Override
    public List<String> shortcutFieldOrder() {
        //- MyHeader=bbb,cccc
        //[bbb,cccc]
        //bbb赋值给MyConfig#key
        //cccc赋值给MyConfig#value
        return Arrays.asList("token");
    }
}